<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 2017/8/16
 * Time: 12:41
 */

header("Content-type: text/html; charset=utf-8");
//define("URL","http://114.242.38.225:8081");
define("URL","http://api.ekjmall.com");

class CrossBorderCatAction extends Action{
    //获得商品信息
    public function synchroProductInfo($good_id,$appid,$name,$key,$url=URL){
        $url = $url.'/GoodsInfor/getGoodsCon';
        $access_token = self::getAccessToken($appid,$key,$name);
        $fields['app_id'] = $appid;
        $fields['access_token'] = $access_token;
        $fields['goods_id'] =$good_id;
        $fields = urldecode(json_encode($fields));
        $ch = curl_init($url) ;
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch) ;
        curl_close($ch);
        $result = json_decode($result,true);
        if($result&&$result['status']==200){
            //调用商品格式化
            $proInfo = self::proInfoFormat($result['data']);
            return $proInfo;
        }else{
            Log::write("同步商品失败   ".$result['message']);
            return false;
        }
    }
    //商品信息格式化
    public function proInfoFormat($data){
        $proInfo['supplierSkuNo'] = $data['goods_id'];
        $proInfo['serialNumber'] = $data['bar_code'];
        $proInfo['name'] = $data['goods_name'];
        $proInfo['foreignName'] = $data['goods_name_en'];
        $proInfo['brand'] = $data['brand_name'];
        $proInfo['fixedPrice'] = $data['market_price'];
        if($data['weight_unit']==2){
            $proInfo['weight'] = $data['gross_weight']/1000;
        }else{
            $proInfo['weight'] = $data['gross_weight'];
        }
        $proInfo['tariff'] = $data['tariff_rate'];
        $proInfo['description'] = $data['description'];
        $proInfo['supportSpecification'] = $data['is_spec'];
        $proInfo['picUrl'] = $data['image'][0];
        $proInfo['webPageContent'] = $data['goods_desc'];
        $proInfo['country'] = $data['goods_origin'];
        $proInfo['specification'] = $data['packing_method'];
        $proInfo['specificUnit'] = $data['metering'];
        $proInfo['categoryId'] = 0;
        $proInfo['createTime'] = date("Y-m-d H:i:s");
        $proInfo['updateTime'] = date("Y-m-d H:i:s");
        $proInfo['proSource']  = 1;
        $proInfo['deleted'] = 0;
        return $proInfo;
    }

    //同步订单
    public function synchroOrder($content,$appid,$name,$key,$url=URL){
          //调用公共平台获得订单信息
            $content = self::kjmOrderFormat($content);
            $url = $url.'/InSale/putOrder/';
            $access_token = self::getAccessToken($appid,$key,$name);
            $content['app_id'] = $appid;
            $content['access_token'] = $access_token;
            $fields = urldecode(json_encode($content));
            $ch = curl_init($url) ;
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $result = curl_exec($ch) ;
            curl_close($ch);
            $result = json_decode($result,true);
            if($result&&$result['status']==200){
                return $result['data'];
            }else{
                Log::write("同步订单失败   ".$result['message']);
                return false;
            }
    }
    //库存同步
    public function synchroStock($appid,$name,$key,$url=URL){
        $url = $url.'/GoodsInfor/getGoodsStock/';
        $access_token = self::getAccessToken($appid,$key,$name);
        $fields['app_id'] = $appid;
        $fields['access_token'] = $access_token;
        $fields = urldecode(json_encode($fields));
        $ch = curl_init($url) ;
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch) ;
        curl_close($ch);
        $result = json_decode($result,true);
        if($result&&$result['status']==200){
            return $result['data'];
        }else{
            Log::write("同步订单失败   ".$result['message']);
            return false;
        }
    }
    //物流同步
    public function synchroLogistics($content,$appid,$name,$key,$url=URL){
        //调用公共平台获得订单信息
        $url = $url.'/InSale/getOrderStatus/';
        $access_token = self::getAccessToken($appid,$key,$name);
        $content['app_id'] = $appid;
        $content['access_token'] = $access_token;
        $fields = urldecode(json_encode($content));
        $ch = curl_init($url) ;
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch) ;
        curl_close($ch);
        $result = json_decode($result,true);
        if($result&&$result['status']==200){
            $data['LogisticName'] = self::getPostCode($result['data']['logisticsCode']);
            $data['LogisticNo'] = $result['data']['logisticsNo'];
            return $data;
        }else{
            Log::write("同步运单失败   ".$result['message']);
            return false;
        }
    }

    //快递公司编码配置
    private function getPostCode($logisticsCode){
        switch($logisticsCode){
            case '1':
                return 'EMS';
                break;
            case '2':
                return '申通快递';
                break;
            case '3':
                return '圆通速递';
                break;
            case '4';
                return '顺丰快递';
                break;
            case '5':
                return '韵达快递';
                break;
            case '14':
                return '中通速递';
                break;
            case '20':
                return '汇通速递';
                break;
            case '21':
                return '民航快递';
                break;
        }
    }
//跨境猫订单数据参数整理
    private function kjmOrderFormat($data){
        $temp = null;
        $orderList = null;
        $temp['orderNo'] = $data['serialNumber'];
        $temp['orderTime'] = date("Y-m-d",strtotime($data['payTime']));
        $temp['purchaseModel'] = '4';
        $temp['charge'] = $data['price'];
        $temp['tax'] = 0;
        $temp['consignee'] = $data['fullName'];
        if($data['province']){
            $addr['province'] = $data['province'];
            $addr['city'] = $data['city'];
            $addr['area'] = $data['country'];
            $addr['street'] = $data['address'];
        }else{
            $addr = R("Product/addressFomate",array($data['address']));
        }
        $addr['street']?$temp['consigneeAddress'] = $addr['street']:$temp['consigneeAddress'] = '未知';
        $temp['consigneeTelephone'] = $data['phone'];
        $addr['province']?$temp['consigneeprov'] = $addr['province']:$temp['consigneeprov'] = '未知';
        $addr['city']?$temp['consigneecity'] = $addr['city']:$temp['consigneecity'] = '未知';
        $addr['area']?$temp['consigneecounty'] = $addr['area']:$temp['consigneecounty'] = '未知';
        $temp['consigneeCountry'] = '398';
        $temp['idType'] = 1;
        $temp['customerId'] = $data['IDNumber'];
        $temp['ieType'] = 'I';
        $temp['modifyMark'] = 1;
        $price = 0;
        foreach($data['proList'] as $i=>$pro){
            $p = null;
            $p['goodsId'] = $pro['supplierSkuNo'];
            $p['goodsNo'] = $pro['serialNumber'];
            $p['goodsName'] = $pro['name'];
            $p['price'] = $pro['price'];
            $price+=$pro['price']*$pro['count'];
            $p['currency'] = '15';
            $p['quantity'] = $pro['count'];
            $p['goods_tax_price'] = '0';
            $orderList[] = $p;
        }
        $temp['goodsValue'] = $price;
        $tempOrder['orderHead']=$temp;
        $tempOrder['orderList']=$orderList;
        $order['order'] = $tempOrder;
        return $order;
    }

    //获得 access_token
    private function getAccessToken($appid,$key,$name){
        return md5($appid.$key.$name);
    }


}